var comm = require("comm")
cc.Class({
    extends: comm,

    properties: {
    },

    onLoad(){
        this.muing2 = false;
        this.jining2 = false;
        this.jining2_uuids = [];
        this.time = 0;//计时，用于update中判断1s时间
        //敌人初始化
        this.enemyData = this.deepClone(G_wuxingfenghuolu.configJson.enemy["1"]);
        this.enemyData.shuxing = this.random(1,5);
        this.enemyData.maxBlood = this.enemyData.blood  
    },
    getData(){
        return this.enemyData;
    },

    action_shui1(node,lv){
        this.enemyData.def = this.enemyData.def*0.9
    },

    action_mu2(node){
        this.muing2 = true;
    },

    action_jin2(lv,cn){
        if(this.jining2==true){
            return;
        }
        this.jining2_uuids.push(cn.uuid)
        var c = 0;
        for(var i = 0;i<this.jining2_uuids.length;i++){
            if(this.jining2_uuids[i]==cn.uuid){
                c++;
            }
        }
        var t = lv*0.3+0.5;
        t -= c*0.1;
        if(t<0.1){
            t = 0.1;
        }
        this.node.getChildByName("xuanyun").active = true;
        this.jining2 = true;
        var self = this;
        this.scheduleOnce(function(){
            self.jining2 = false;
            self.node.getChildByName("xuanyun").active = false;
        },t);
    },

    getIsMu2ing(){
        return this.muing2;
    },

    clearState_muing(){
        this.muing2 = false;
    },
   

    

    

    update (dt) {
        this.time += dt;
        if(this.time>0.1){
            this.time = 0;
            this.beginMove();
            this.disA();
        }
    },

    beginMove(){
        if(this.isDie==true){
            return;
        }
         if(this.muing2==true){
            return;
        }
        if(this.jining2==true){
            return;
        }

        this.node.opacity = 255;
        this.node.angle = -this.lookAtObj();
        var angle = (this.node.angle) / 180 * Math.PI;
        //合成基于 X正方向的方向向量
        var dir = cc.v2(Math.cos(angle),Math.sin(angle));
        //单位化向量
        dir.normalizeSelf();
    
        this.node.x += this.enemyData.speed*dir.x/20
        this.node.y += this.enemyData.speed*dir.y/20
    
        if(dir.x<0){
            this.node.scaleX = 1;
        }else{
            this.node.scaleX = -1;
        }
        this.node.angle = 0;
    },
    lookAtObj(){
        //计算出朝向
        var dx = 0 - this.node.x;
        var dy = 0 - this.node.y;
        var dir = cc.v2(dx,dy);
        //根据朝向计算出夹角弧度
        var angle = dir.signAngle(cc.v2(1,0));
        //将弧度转换为欧拉角
        var degree = angle / Math.PI * 180;
        //赋值给节点
        return degree;
    },
    disA(){
        var mOm = this.node.position.sub(cc.v2(0,0))
        var tmpDis = Math.abs(mOm.mag());
        if(tmpDis<20){
        
        }
    },

    getDie(){
        return this.isDie;
    },

    reduceBlood(data){
        if(this.isDie==true){
            return;//已经死亡
        }
        this.enemyData.blood -= 10;

        // var prefab_reduceBlood = cc.instantiate(cc.globalRes["prefab_reduceBlood"]);
        // prefab_reduceBlood.position = this.node.position;
        // prefab_reduceBlood.getComponent("reduceBlood").setInfo(c_hurt,this.node.position,isbaoji);
        // TONGYONG.mainObj.zongmenNode.addChild(prefab_reduceBlood);

        // if(this.enemyData.blood>0){
        //     this.attedAnim();//被攻击动画
        //     return;
        // }

    },

    isDieAnim(){
        var self = this;
        var callFunc = cc.callFunc(
            function(){
                self.node.destroy();
            }
        )
        var action1 = cc.scaleTo(1,0.1,0.1);
        var se = cc.sequence(action1, callFunc);
        this.node.runAction(se);
    },


});
